<?
include('base.php');
User::protect();
$section = 'band_information';
include_class('band_information');
include_class('locations');

$bi = BandInformation::get();

if ($_GET['task'] == 'update') {
	$res = $bi->update($_POST);
	if (!db::isError($res)) {
		header('Location: band_information.php');
		exit;
	}
}
if ($_GET['task'] == 'edit' || $_GET['task'] == 'update') { 
	$editors = array('bio', 'miscellaneous', 'description');
}
$page_title = 'Edit Band Bio/Information';
include('layout/header.php');

?>

<div id="breadcrumb">
	<a href="index.php">Audition&nbsp;&#62;</a>&nbsp;<a href="band.php">Manage&nbsp;Band&nbsp;&#62;</a>&nbsp;Band Information
</div>

<? if (db::isError($res)) {
	$res->outputList();
}

if (($_GET['task'] == 'edit' || $_GET['task'] == 'update') && User::isAdmin()) { 

	$name = ($_POST) ? $_POST['name'] : $bi->getName();
	$genreID = ($_POST) ? $_POST['genreID'] : $bi->getGenreID();
	$bio = ($_POST) ? $_POST['bio'] : $bi->getBio();
	$miscellaneous = ($_POST) ? $_POST['miscellaneous'] : $bi->getMiscellaneous();
	$defaultCity = ($_POST) ? $_POST['defaultCity'] : $bi->getDefaultCity();
	$defaultStateProvince = ($_POST) ? $_POST['defaultStateProvince'] : $bi->getDefaultStateProvince();
	$defaultCountry = ($_POST) ? $_POST['defaultCountry'] : $bi->getDefaultCountry();
	$city = ($_POST) ? $_POST['city'] : $bi->getCity();
	$address1 = ($_POST) ? $_POST['address1'] : $bi->getAddress1();
	$address2 = ($_POST) ? $_POST['address2'] : $bi->getAddress2();	
	$managerName = ($_POST) ? $_POST['managerName'] : $bi->getManager();
	$stateProvince = ($_POST) ? $_POST['stateProvince'] : $bi->getStateProvince();
	$postalCode = ($_POST) ? $_POST['postalCode'] : $bi->getPostalCode();
	$country = ($_POST) ? $_POST['country'] : $bi->getCountry();
	$description = ($_POST) ? $_POST['description'] : $bi->getDescription();
?>

	<h1>edit band information:</h1>
	<div class="inset">
	<form id="edit_entry" action="<?=$PHP_SELF?>?task=update" method="post">
	<table border="0" class="edit-form" cellspacing="0" cellpadding="0">
	<tr>
		<td width="50%"><strong>Name</strong><br/>
		<input type="text" class="text" name="name" value="<?=$name?>"></td>

		<td width="50%"><strong>Genre</strong><br/>
		<select name="genreID">
		<?
		
		$genres = $bi->getGenres();
		foreach($genres as $genre) {
			echo('<option value="' . $genre['ID'] . '" ');
			if ($genre['ID'] == $genreID) {
				echo('selected');
			}
			echo('>' . $genre['genreName'] . '</option>');
		} ?>
		</select>
		</td>
	</tr>
	<tr>
		<td colspan="2"><strong>Bio</strong><br/>
		<textarea name="bio" id="bio" class="full"><?=$bio?></textarea>
		</td>
	</tr>
	<tr>
		<td colspan="2"><strong>Miscellaneous</strong><br/>
		<textarea name="miscellaneous" id="miscellaneous" class="full"><?= $miscellaneous ?></textarea>
		</td>
	</tr>
	</table>
	<table class="edit-form" border="0" cellspacing="0" cellpadding="0">
	<tr>
		<td colspan="3"><strong>Default Location</strong><br/>This information determines the default city, state/province and country for your shows' venues. This can be helpful if you're a local artist/band, and most of your shows are played in the same area.</td>
	</tr>
	<tr>
		<td valign="top" width="33%"><strong>City</strong><br/>
		<input class="text" type="text" name="defaultCity" value="<?=$defaultCity?>"></td>
		<td valign="top" width="34%"><strong>State/Province</strong><br/>
		<select id="defaultStateProvince" name="defaultStateProvince" onchange="toggleOther(document.forms[0].defaultStateProvinceOther, this)">
		<? $sl = new StateList();
		$stateList = $sl->getStates();
		$selected = false;
		foreach ($stateList as $key => $sp) {
			echo("<option value=\"" . $key . "\"");
			if ($defaultStateProvince == $key) {
				$selected = true;
				echo(" selected");
			}
			echo(">" . $sp . "</option>\r");
		} 
		echo("<option value=\"??\"");
		if (!$selected) { 
			echo(" selected");
		}
		echo(">** OTHER</option>");
		?>
		</select>
		<br/>
		<input class="text" <? if ($selected) { ?> disabled <? } ?> type="text" name="defaultStateProvinceOther" value="<? if (!$selected) { ?><?=$defaultStateProvince?><? } ?>">
		</td>
		<td valign="top" width="33%"><strong>Country</strong><br/>
		<select name="defaultCountry">
		<? $sl = new CountryList();
		$countryList = $sl->getCountries();
		$selected = false;
		foreach ($countryList as $key => $co) {
			echo("<option value=\"" . $key . "\"");
			if ($defaultCountry == $key) {
				$selected = true;
				echo(" selected");
			}
			echo(">" . $co . "</option>\r");
		} 
		?>
		</select>
		</td>
	</tr>
	</table>
	<table class="edit-form" cellspacing="0" cellpadding="0" border="0">
	<tr>
		<td colspan="3"><strong>Contact Information</strong><br/>Your manager's name (if applicable) and an address where your band can be contacted.</td>
	</tr>
	<tr>
		<td colspan="2"><strong>Manager</strong><br/>
		<input type="text" class="text" name="managerName" value="<?=$managerName?>"></td>
		<td>&nbsp;</td>
	</tr>
	<tr>
		<td width="33%"><strong>Address 1</strong><br/>
		<input class="text" type="text" name="address1" value="<?=$address1?>"></td>
		<td width="34%"><strong>Address 2</strong><br/>
		<input class="text" type="text" name="address2" value="<?=$address2?>"></td>
		<td width="33%"><strong>City</strong><br/>
		<input type="text" class="text" name="city" value="<?=$city?>"></td>
	</tr>
	<tr>
		<td valign="top"><strong>State/Province</strong><br/>
		<select id="stateProvince" name="stateProvince" onchange="toggleOther(document.forms[0].stateProvinceOther, this)">
		<? $sl = new StateList();
		$stateList = $sl->getStates();
		$selected = false;
		foreach ($stateList as $key => $sp) {
			echo("<option value=\"" . $key . "\"");
			if ($stateProvince == $key) {
				$selected = true;
				echo(" selected");
			}
			echo(">" . $sp . "</option>\r");
		} 
		echo("<option value=\"??\"");
		if (!$selected) { 
			echo(" selected");
		}
		echo(">** OTHER</option>");
		?>
		</select>
		<br/>
		<input class="text" <? if ($selected) { ?> disabled <? } ?> type="text" name="stateProvinceOther" value="<? if (!$selected) { ?><?=$stateProvince?><? } ?>">
		</td>
		<td valign="top"><strong>Zip/Postal Code</strong><br/>
		<input class="text" type="text" name="postalCode" value="<?=$postalCode?>"></td>
		<td valign="top"><strong>Country</strong><br/>
		<select id="country" name="country">
		<? $sl = new CountryList();
		$countryList = $sl->getCountries();
		$selected = false;
		foreach ($countryList as $key => $co) {
			echo("<option value=\"" . $key . "\"");
			if ($country == $key) {
				$selected = true;
				echo(" selected");
			}
			echo(">" . $co . "</option>\r");
		} 
		?>
		</select>
		</td>
	</tr>
	<tr>
		<td colspan="3"><strong>Description</strong><br/>
		<textarea name="description" id="description" class="full"><?= $description ?></textarea>
		</td>
	</tr>
	</table>
	<input type="submit" value="Update Information" class="button" name="submit">
	<input type="submit" value="Exit Without Updating" class="button" name="preview" onclick="location.href='band_information.php'; return false">
	</form>
	</div>
	
	<script type="text/javascript">
		function toggleOther(obj, sp) {
			if (sp.value == "??") { 
				obj.disabled = false;
			} else {
				obj.disabled = true;
				obj.value = "";
			}
		}
	</script>

<? } else { ?>

	<h1>view band information:</h1>
	<div class="inset">
		<? if ($bi->getID() > 0) { ?>
			<h2><?=$bi->getName()?></h2>
			<span class="small">
				Genre: <?=$bi->getGenreName()?><br>
			</span>
			<br>
			<strong>Bio:</strong><br/>
			<? if ($bi->getBio()) {
				echo($bi->getBio());
			} else {
				echo("None");
			} ?>
			<br/><br/>
			<strong>Contact Information</strong><br/>
			<?=$bi->getAddress1()?><br/>
			<? 
			if ($bi->getAddress2()) {
				echo($bi->getAddress2() . '<br/>');
			}
			if ($bi->getCity()) {
				echo($bi->getCity());
			} else {
				echo("(no city)");
			}
			echo(", ");
			if ($bi->getStateProvince()) {
				echo($bi->getStateProvince());
			} else {
				echo("(no state/province)");
			}
			echo("&nbsp;&nbsp;");
			if ($bi->getPostalCode()) {
				echo($bi->getPostalCode());
			} else {
				echo("(no zip/postal)");
			}
			echo("<br/>");
			echo($bi->getCountryName());
			echo('<br/><br/>');
			?>
			<strong>Manager</strong><br/>
			<? if ($bi->getManager()) {
				echo($bi->getManager());
			} else {
				echo("None");
			} ?>
			<br/><br/>
		<? } else { ?>
			Band information has not yet been populated. Please, click the "edit information" below, and specify your band's name and location.
			<br/><br/>
		<? } ?>
		<form name="entry_status" method="get">
			<? if (User::isAdmin()) { ?>
				<input type="submit" name="" value="Edit Information" onclick="location.href='<?=$PHP_SELF?>?task=edit';return false" class="button">
			<? } ?>
			<input type="submit" value="Return to Band Menu" name="" onclick="location.href='band.php'; return false" class="button">
		</form>
		
	</div>

<? } ?>

<? include('layout/footer.php'); ?>
